package com.tuanzi.loan.business.repository.compensation.impl;

import com.tuanzi.loan.business.repository.compensation.CompensationChildOrderRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.compensation.CompensationChildOrderRequest;
import com.tuanzi.loan.vo.response.compensation.CompensationChildOrderResponse;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.Calendar;
import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCardCompensationOrder.LOAN_CARD_COMPENSATION_ORDER;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCompensationChildOrder.LOAN_COMPENSATION_CHILD_ORDER;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCompensationCompanyInfo.LOAN_COMPENSATION_COMPANY_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCompensationUserCard.LOAN_COMPENSATION_USER_CARD;

/**
 * @author caiwanlin
 * @create 2019-02-13 10:56
 */
@Repository
public class CompensationChildOrderRepositoryImpl extends JOOQBaseRepositoryPlus implements CompensationChildOrderRepository {
    @Override
    protected TableImpl getTable() {
        return LOAN_COMPENSATION_CHILD_ORDER;
    }

    @Override
    public Page<CompensationChildOrderResponse> list(CompensationChildOrderRequest searchRequest, Pageable pageable) {
        Date endTime = searchRequest.getEndTime();
        if (endTime != null){
            Calendar instance = Calendar.getInstance();
            instance.setTime(endTime);
            instance.set(Calendar.HOUR_OF_DAY,23);
            instance.set(Calendar.MINUTE,59);
            instance.set(Calendar.SECOND,59);
            endTime = instance.getTime();
        }
        fields(LOAN_COMPENSATION_CHILD_ORDER.ID,LOAN_COMPENSATION_CHILD_ORDER.PARENT_ORDER_ID,
                LOAN_COMPENSATION_CHILD_ORDER.PARTY_ORDER_ID,LOAN_COMPENSATION_CHILD_ORDER.COMPANY_TYPE,
                LOAN_COMPENSATION_CHILD_ORDER.TYPE,LOAN_COMPENSATION_CHILD_ORDER.BUSINESS_TYPE,
                LOAN_COMPENSATION_CHILD_ORDER.STATUS,LOAN_COMPENSATION_CHILD_ORDER.FEE,
                LOAN_COMPENSATION_CHILD_ORDER.CREATE_TIME, LOAN_COMPENSATION_USER_CARD.CARD_NUM,
                LOAN_COMPENSATION_USER_CARD.BANK_MOBILE_NUM,LOAN_COMPENSATION_USER_CARD.BANK,
                LOAN_COMPENSATION_USER_CARD.CARD_TYPE, LOAN_CARD_COMPENSATION_ORDER.MOBNUM,
                LOAN_CARD_COMPENSATION_ORDER.PETNAME, LOAN_COMPENSATION_COMPANY_INFO.COMPANY_NAME,
                LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM,LOAN_COMPENSATION_CHILD_ORDER.REMARK,
                LOAN_CARD_COMPENSATION_ORDER.AMOUNT);
        leftJoin(LOAN_COMPENSATION_USER_CARD,LOAN_COMPENSATION_CHILD_ORDER.CARD_ID.eq(LOAN_COMPENSATION_USER_CARD.ID));
        leftJoin(LOAN_CARD_COMPENSATION_ORDER,LOAN_COMPENSATION_CHILD_ORDER.PARENT_ORDER_ID.eq(LOAN_CARD_COMPENSATION_ORDER.ID));
        leftJoin(LOAN_COMPENSATION_COMPANY_INFO,LOAN_COMPENSATION_CHILD_ORDER.COMPANY_TYPE.eq(LOAN_COMPENSATION_COMPANY_INFO.COMPANY_TYPE));
        eq(LOAN_COMPENSATION_CHILD_ORDER.PARENT_ORDER_ID,searchRequest.getParentOrderId());
        eq(LOAN_CARD_COMPENSATION_ORDER.MOBNUM,searchRequest.getPhone());
        contains(LOAN_CARD_COMPENSATION_ORDER.PETNAME,searchRequest.getPetname());
        between(LOAN_COMPENSATION_CHILD_ORDER.CREATE_TIME,searchRequest.getStartTime(),endTime);
        orderBy(LOAN_COMPENSATION_CHILD_ORDER.ID, Sort.Direction.DESC);
        Page<CompensationChildOrderResponse> search = search(pageable, e -> {
            CompensationChildOrderResponse response = new CompensationChildOrderResponse();
            response.setId(e.get(LOAN_COMPENSATION_CHILD_ORDER.ID));
            response.setParentOrderId(e.get(LOAN_COMPENSATION_CHILD_ORDER.PARENT_ORDER_ID));
            response.setPartyOrderId(e.get(LOAN_COMPENSATION_CHILD_ORDER.PARTY_ORDER_ID));
            response.setCompanyType(e.get(LOAN_COMPENSATION_CHILD_ORDER.COMPANY_TYPE));
            response.setType(e.get(LOAN_COMPENSATION_CHILD_ORDER.TYPE));
            response.setBusinessType(e.get(LOAN_COMPENSATION_CHILD_ORDER.BUSINESS_TYPE));
            response.setStatus(e.get(LOAN_COMPENSATION_CHILD_ORDER.STATUS));
            response.setFee(e.get(LOAN_COMPENSATION_CHILD_ORDER.FEE));
            response.setCreateTime(e.get(LOAN_COMPENSATION_CHILD_ORDER.CREATE_TIME));
            response.setCardNum(PhoneNumNideUtil.hideBankCard(e.get(LOAN_COMPENSATION_USER_CARD.CARD_NUM)));
            response.setBankMobileNum(PhoneNumNideUtil.hideNum(e.get(LOAN_COMPENSATION_USER_CARD.BANK_MOBILE_NUM)));
            response.setBankName(e.get(LOAN_COMPENSATION_USER_CARD.BANK));
            response.setCardType(e.get(LOAN_COMPENSATION_USER_CARD.CARD_TYPE));
            response.setPhone(PhoneNumNideUtil.hideNum(e.get(LOAN_CARD_COMPENSATION_ORDER.MOBNUM)));
            response.setPetname(PhoneNumNideUtil.hideName(e.get(LOAN_CARD_COMPENSATION_ORDER.PETNAME)));
            response.setChannelFrom(e.get(LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM));
            response.setCompanyName(e.get(LOAN_COMPENSATION_COMPANY_INFO.COMPANY_NAME));
            response.setRemark(e.get(LOAN_COMPENSATION_CHILD_ORDER.REMARK));
            response.setAmount(e.get( LOAN_CARD_COMPENSATION_ORDER.AMOUNT));
            return response;
        });
        return search;
    }
}
